package com.gr.smartedubackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gr.smartedubackend.common.BaseResponse;
import com.gr.smartedubackend.common.BusinessException;
import com.gr.smartedubackend.common.DeleteRequest;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.model.dto.exam.ExamAddRequest;
import com.gr.smartedubackend.model.dto.exam.ExamQueryRequest;
import com.gr.smartedubackend.model.dto.exam.ExamUpdateRequest;
import com.gr.smartedubackend.model.entity.Course;
import com.gr.smartedubackend.model.entity.Exam;
import com.gr.smartedubackend.model.entity.StudentGroup;
import com.gr.smartedubackend.service.CourseService;
import com.gr.smartedubackend.service.ExamService;
import com.gr.smartedubackend.service.StudentGroupService;
import com.gr.smartedubackend.utils.ResultUtils;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/exam")
public class ExamController {

    @Resource
    private ExamService examService;

    @Resource
    private StudentGroupService studentGroupService;

    @Resource
    private CourseService courseService;

    @PostMapping("/add")
    public BaseResponse<Long> addExam(@RequestBody ExamAddRequest examAddRequest) {
        ThrowUtils.throwIf(examAddRequest == null, ErrorCode.PARAMS_ERROR);
        Long classId = examAddRequest.getClassId();
        Long courseId = examAddRequest.getCourseId();
        if (classId == null || classId <= 0 || courseId == null || courseId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        StudentGroup studentGroup = studentGroupService.getById(classId);
        if (studentGroup == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "班级不存在");
        }
        Course course = courseService.getById(courseId);
        if (course == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "课程不存在");
        }
        examAddRequest.setClassName(studentGroup.getClassName());
        examAddRequest.setCourseName(course.getCourseName());
        Exam exam = new Exam();
        BeanUtils.copyProperties(examAddRequest, exam);
        boolean result = examService.save(exam);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(exam.getId());
    }

    @GetMapping("/get")
    public BaseResponse<Exam> getExamById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        Exam exam = examService.getById(id);
        ThrowUtils.throwIf(exam == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(exam);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<Exam>> listExamByPage(@RequestBody ExamQueryRequest examQueryRequest) {
        ThrowUtils.throwIf(examQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = examQueryRequest.getCurrent();
        int pageSize = examQueryRequest.getPageSize();
        Page<Exam> examPage = examService.page(new Page<>(current, pageSize),
                examService.getQueryWrapper(examQueryRequest));
        return ResultUtils.success(examPage);
    }

    @PostMapping("/list")
    public BaseResponse<List<Exam>> listExam(@RequestBody ExamQueryRequest examQueryRequest) {
        ThrowUtils.throwIf(examQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(examService.list(examService.getQueryWrapper(examQueryRequest)));
    }


    @PostMapping("/update")
    public BaseResponse<Boolean> updateExam(@RequestBody ExamUpdateRequest examUpdateRequest) {
        ThrowUtils.throwIf(examUpdateRequest == null || examUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        Long classId = examUpdateRequest.getClassId();
        Long courseId = examUpdateRequest.getCourseId();
        if (classId == null || classId <= 0 || courseId == null || courseId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        StudentGroup studentGroup = studentGroupService.getById(classId);
        if (studentGroup == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "班级不存在");
        }
        Course course = courseService.getById(courseId);
        if (course == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "课程不存在");
        }
        examUpdateRequest.setClassName(studentGroup.getClassName());
        examUpdateRequest.setCourseName(course.getCourseName());
        Exam exam = new Exam();
        BeanUtils.copyProperties(examUpdateRequest, exam);
        boolean result = examService.updateById(exam);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteExam(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean result = examService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }
}
